package com.wsoft.sms.controller;

import com.github.pagehelper.PageInfo;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.sms.entity.SmsMessageEntity;
import com.wsoft.sms.query.message.MessageInsertQuery;
import com.wsoft.sms.query.message.MessageListQuery;
import com.wsoft.sms.query.message.MessageUpdateQuery;
import com.wsoft.sms.service.ISmsMessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 消息表 前端控制器
 * @author zhanggukai
 * @since 2025年01月02日 15:35:55
 */
@RestController
@Tag(name = "消息管理")
@RequestMapping("/sms/message")
public class SmsMessageController {
    @Resource
    private ISmsMessageService messagesService;
    @Operation(summary = "新增消息", description = "新增消息")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid MessageInsertQuery query) {
        boolean b = messagesService.insert(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改消息", description = "修改消息")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid MessageUpdateQuery query) {
        boolean b = messagesService.update(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "发布消息", description = "发布消息")
    @GetMapping("/release")
    public ResponseResult release(@RequestParam("id") Long id) {
        return ResponseResult.success(messagesService.release(id));
    }

    @Operation(summary = "撤销消息", description = "撤销消息")
    @GetMapping("/withdraw")
    public ResponseResult withdraw(@RequestParam("id") Long id) {
        return ResponseResult.success(messagesService.withdraw(id));
    }

    @Operation(summary = "消息详情", description = "消息详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(messagesService.getMessage(id));
    }

    @Operation(summary = "用户获取消息详情", description = "用户获取消息详情")
    @GetMapping("/getByMember")
    public ResponseResult getByMember(@RequestParam("id") Long id) {
        return ResponseResult.success(messagesService.getMessageByMember(id));
    }

    @Operation(summary = "管理消息分页-web端", description = "消息分页-web端")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody MessageListQuery query) {
        PageInfo<SmsMessageEntity> page = messagesService.pageMessage(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "用户消息列表-web端", description = "用户消息列表-web端")
    @PostMapping("/listUnRead")
    public ResponseResult listUnRead(@RequestBody MessageListQuery query) {
        return ResponseResult.success(messagesService.listUnRead(query));
    }

    @Operation(summary = "删除消息", description = "删除消息")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = messagesService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "消息-一键已读", description = "消息-一键已读")
    @PostMapping("/allRead")
    public ResponseResult allRead() {
        boolean b = messagesService.allRead();
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }
}
